﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModeloGaleriaNET;
using System.Threading;
using CADs;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Sockets;

namespace SeguridadGaleria
{
    public class AuditoriaBD: AbstractAuditoria
    {
        public AuditoriaBD()
        { 
        }

        public override void Mensaje(object[] descripcionArgs, int tipo, int u)
        {
            Thread t;
            string mens;
            int zona;

            t = new Thread(delegate()
            {
                mens = new TipoAuditoriaCAD().GetDescripcionAuditoria(tipo);
                zona = Utilities.GetZonaHoraria();
                new AuditoriaCAD().InsertAuditoria(DateTime.UtcNow, string.Format(mens, descripcionArgs), 
                    tipo, Utilities.GetZonaHoraria(), u, GetLocalIP());
            });
            t.Start();
        }

        private string GetLocalIP()
        {
            try
            { 
                IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

                foreach (IPAddress ip in localIPs)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        return ip.ToString();
                }
                return "IP not found";
            }
            catch (Exception)
            {
                return "IP not found";
            }  
        }
    }
}
